package com.galeno.impl;

import com.galeno.dao.HistoryEventDao;
import com.galeno.pojo.CombineCondition;
import com.galeno.pojo.EventCondition;
import com.galeno.pojo.EventStateBean;
import com.galeno.pojo.LogBean;
import org.apache.flink.api.common.state.ListState;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 日期2021/11/16时间20:36
 */
public class HistoryEventStateDaoImpl implements HistoryEventDao {
    private ListState<EventStateBean> eventState;

    public HistoryEventStateDaoImpl(ListState<EventStateBean> eventState) {
        this.eventState = eventState;
    }

    @Override
    public String queryEventSequenceStr(LogBean bean, CombineCondition combineCondition) throws SQLException, Exception {
        //查询条件
        Long startTime = combineCondition.getStartTime();
        Long endTime = combineCondition.getEndTime();
        //预先设置的条件
        List<String> target = combineCondition.getEventConditions()
                .stream()
                .map(EventCondition::getEventId)
                .collect(Collectors.toList());
        StringBuffer sb = new StringBuffer();
        Iterable<EventStateBean> events = eventState.get();
        for (EventStateBean event : events) {
            long timeStamp = event.getTimeStamp();
            if (timeStamp >= startTime && endTime >= timeStamp) {
                //将事件映射成短的字母或者数字
                sb.append(target.indexOf(event.getEventId()) + 1);
            }
        }
        return sb.toString();


    }
}
